package com.hclc.impl.aliyun;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.hclc.entity.model.BaseSmsTemplateModel;
import com.hclc.entity.model.contans.ApiConst;
import com.hclc.api.aliyun.IAliSmsService;
import com.hclc.api.base.IBaseSmsLogService;
import com.hclc.api.base.IBaseSmsTemplateService;
import com.hclc.api.redis.IRedisService;
import com.hclc.entity.dto.SendSmsCodeDto;
import com.hclc.entity.model.BaseSmsLogModel;
import com.hclc.util.json.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 阿里云短信通知相关service
 * @author jsl
 * @date 2020/8/16
 * @return
*/
@Service("aliSmsServiceImpl")
@Slf4j
public class AliSmsServiceImpl extends AliSmsConfig implements IAliSmsService {

	@Autowired
	private IBaseSmsTemplateService baseSmsTemplateService;

	@Autowired
	private IBaseSmsLogService baseSmsLogService;

	@Autowired
	private IRedisService redisService;

	public AliSmsServiceImpl() {

	}

	@Override
	public boolean aliSmsSend(SendSmsCodeDto sendSmsCodeDto) {
 		DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
		IAcsClient client = new DefaultAcsClient(profile);
		//验证码
		int code = (int) ((Math.random() * 9 + 1) * 100000);

		BaseSmsTemplateModel setModel = new BaseSmsTemplateModel();
		setModel.setTemplateType(sendSmsCodeDto.getRedisKeyPrefix());
		BaseSmsTemplateModel model = baseSmsTemplateService.getModel(setModel);
		//判断模板是否有效
		if(null != model && model.getEnable().equals(ApiConst.STATUS_ENABLE) ) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("code", code);
			CommonRequest request = new CommonRequest();
			request.setMethod(MethodType.POST);
			request.setDomain(domain);
 			request.setVersion(version);
			request.setAction(action);
			request.putQueryParameter("SignName", signName);
			request.putQueryParameter("PhoneNumbers", sendSmsCodeDto.getReceiveMobile());
			request.putQueryParameter("TemplateCode", model.getTemplateCode());
			request.putQueryParameter("TemplateParam", JsonUtil.toJson(map));
			try {
				CommonResponse response = client.getCommonResponse(request);
				Map<String, Object> responseMap = JsonUtil.toMap(response.getData());
				if (responseMap.containsKey("Code")) {
					String status = responseMap.get("Code").toString();
					if ("OK".equals(status)) {
						//存入redis
						redisService.set(sendSmsCodeDto.getRedisKeyPrefix()+sendSmsCodeDto.getReceiveMobile(), JsonUtil.toJson(code), 15L, TimeUnit.MINUTES);
						//存入发送短信记录
						BaseSmsLogModel baseSmsLogModel = new BaseSmsLogModel();
						baseSmsLogModel.setName(model.getTemplate());
						baseSmsLogModel.setTitle(model.getRemark());
						baseSmsLogModel.setRecipient(sendSmsCodeDto.getReceiveMobile());
						baseSmsLogModel.setContent("【"+model.getRemark()+"】:"+model.getSendContent().replace("${code}", JsonUtil.toJson(code)));
						baseSmsLogModel.setCreateTime(LocalDateTime.now());
						baseSmsLogService.addBaseSmsLog(baseSmsLogModel);
						return true;
					}
				}
			} catch (ClientException e) {
				e.printStackTrace();
			}
		}else {
			log.warn("该模版[" + model.getRemark() + "]已经被禁用！");
		}
		return false;
	}

}
